/*  This file is part of -_-.

    -_- is free software: you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation, either version 3 of the License, or
    (at your option) any later version.

    -_- is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with -_-.  If not, see <http://www.gnu.org/licenses/>.

    Copyright 2012-2013 Oliver Katz */

#include "pane.h"

namespace sleepyface
{
    namespace visual
    {
        Region PaneManager::calculateScrollbars(Renderer &renderer, int r)
        {
            Region sr = renderer.getSurface(r).getRegion();
            Region v = visibleRegion(r);

            int vw = v.getWidth()-scrollbarPadding*2;
            int w = (vw*vw)/sr.getWidth();
            if (w < scrollbarMinimum)
                w = scrollbarMinimum;
            int vh = v.getHeight()-scrollbarPadding*2;
            int h = (vh*vh)/sr.getHeight();
            if (h < scrollbarMinimum)
                h = scrollbarMinimum;
            int vx = v.getX0()+v.getWidth()/2;
            int x = vx+(scrollbarPadding*(vw-w))/v.getWidth();
            int vy = v.getY0()+v.getHeight()/2;
            int y = vy+(scrollbarPadding*(vh-h))/v.getHeight();
            return coordSizeBox(x, y, w, h);
        }

        Region PaneManager::calculateVisibleRegionFromScrollbars(Renderer &renderer, int r, Region scrolls)
        {
            Region v = visibleRegion(r);
            Region sr = renderer.getSurface(r).getRegion();

            int vw = v.getWidth()-scrollbarPadding*2;
            int w = (vw*vw)/sr.getWidth();
            if (w < scrollbarMinimum)
                w = scrollbarMinimum;
            int vh = v.getHeight()-scrollbarPadding*2;
            int h = (vh*vh)/sr.getHeight();
            if (h < scrollbarMinimum)
                h = scrollbarMinimum;
            int vx = scrolls.getX0()*vw-(scrollbarPadding*(vw-w));
            int vy = scrolls.getY0()*vw-(scrollbarPadding*(vh-h));
            return offsetBox(vx-v.getWidth()/2, vy-v.getHeight()/2);
        }

        PaneManager::PaneManager()
        {
        }

        int PaneManager::append(Region vis, Region scr)
        {
            if (!scr.inside(vis))
                return -1;
            if (scr.getWidth() <= scrollbarPadding*2 || scr.getHeight() <= scrollbarPadding*2)
                return -2;

            pair<Region, Region> tmp;
            tmp.first = vis;
            tmp.second = scr;

            regions.push_back(tmp);
            return regions.size();
        }

        Region &PaneManager::visibleRegion(int r)
        {
            return regions[r].first;
        }

        Region &PaneManager::screenRegion(int r)
        {
            return regions[r].second;
        }

        string PaneManager::getSurfaceNameFromCoord(Renderer &renderer, Region coord)
        {
            for (vector<pair<Region, Region> >::iterator i = regions.begin(); i != regions.end(); i++)
            {
                if (screenRegion(*i).within(coord))
                    return *i;
            }

            return "";
        }

        int PaneManager::render(Renderer &renderer)
        {
            vector<string> surfs = renderer.listSurfaces();

            for (vector<string>::iterator i = surfs.begin(); i != surfs.end(); i++)
            {
                renderer.getSurface(*i).rect(coordBox(visibleRegion(*i).getX0(), visibleRegion(*i).getY0(), visibleRegion(*i).getX0()+scrollbarPadding, visibleRegion(*i).getY1()), scrollbarColorBG);
                renderer.getSurface(*i).rect(coordBox(visibleRegion(*i).getX1()-scrollbarPadding, visibleRegion(*i).getY0(), visibleRegion(*i).getX1(), visibleRegion(*i).getY0()), scrollbarColorBG);
                renderer.getSurface(*i).rect(coordBox(visibleRegion(*i).getX0(), visibleRegion(*i).getY0(), visibleRegion(*i).getX1(), visibleRegion(*i).getY0()+scrollbarPadding), scrollbarColorBG);
                renderer.getSurface(*i).rect(coordBox(visibleRegion(*i).getX0(), visibleRegion(*i).getY1()-scrollbarPadding, visibleRegion(*i).getX1(), visibleRegion(*i).getY1()), scrollbarColorBG);
                Region scrolls = calculateScrollbars(renderer, *i);
                renderer.getSurface(*i).rect(coordSizeBox(scrolls.getX0(), visibleRegion(*i).getY1()-(scrollbarPadding-scrollbarThickness)*0.5, scrolls.getX1(), visibleRegion(*i).getY1()-(scrollbarPadding+scrollbarThickness)*0.5), scrollbarColorFG);
                renderer.getSurface(*i).rect(coordSizeBox(visibleRegion(*i).getX1()-(scrollbarPadding-scrollbarThickness)*0.5, scrolls.getY0(), visibleRegion(*i).getX1()-(scrollbarPadding+scrollbarThickness)*0.5, scrolls.getY1()), scrollbarColorFG);
                renderer.getSurface(*i).renderTo(renderer.getPrimary(), visibleRegion(*i), screenRegion(*i));
            }

            return 0;
        }
    }
}